﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Forms;

    /// <summary>
    /// Interaction logic for SelectFolderControl.xaml
    /// </summary>
    public partial class SelectFolderControl : System.Windows.Controls.UserControl
        {

        public string DialogPrompt { get; set; }

        public event EventHandler FolderNameChanged;

        public string FolderName
            {
            get { return FolderNameTextBox.Text; }
            set { FolderNameTextBox.Text = value; }
            }

        public SelectFolderControl()
            {
            this.DialogPrompt = "Folder Selection";
            InitializeComponent();
            }

        public static string RequestFolderName(string prompt)
            {

            // Create a dialog box
            using (var dialog = new FolderBrowserDialog())
                {

                // Configure the dialog box
                dialog.Description = prompt;
                dialog.ShowNewFolderButton = true;

                // Show the dialog
                var result = dialog.ShowDialog();
                if (result == DialogResult.OK)
                    {
                    return dialog.SelectedPath;
                    }
                else
                    {
                    return null;
                    }

                }

            }

        private void SelectFolderButton_Click(object sender, RoutedEventArgs e)
            {

            // Create a dialog box
            using (var dialog = new FolderBrowserDialog())
                {

                // Configure the dialog box
                dialog.Description = DialogPrompt;
                dialog.ShowNewFolderButton = true;
                dialog.SelectedPath = FolderNameTextBox.Text;

                // Show the dialog
                var result = dialog.ShowDialog();
                if (result == DialogResult.OK)
                    {

                    // Update the file name
                    FolderNameTextBox.Text = dialog.SelectedPath;

                    }

                }

            }

        private void FolderNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
            if (FolderNameChanged != null) FolderNameChanged(this, EventArgs.Empty);
            }

        }

    }
